package com.example.demo.controller.admin;

import com.example.demo.Util.Result;
import com.example.demo.Util.ResultEnum;
import com.example.demo.Util.ResultUtil;
import com.example.demo.Util.redisUtil.RedisLockTemplate;
import com.example.demo.Util.redisUtil.RedisService;
import com.example.demo.mapper.MaterialAppealMap;
import com.example.demo.mapper.MaterialMap;
import com.example.demo.po.Class;
import com.example.demo.po.Material;
import com.example.demo.po.MaterialAppeal;
import com.example.demo.po.dto.ClassParam;
import com.example.demo.po.dto.MaterialAppealParam;
import com.example.demo.service.MaterialAppealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/materialAppeal")
public class MaterialAppealController {

    @Autowired
    MaterialAppealMap materialAppealMap;

    @Autowired
    MaterialMap materialMap;


    @Autowired
    MaterialAppealService materialAppealService;

    @Autowired
    RedisService redisService;

    @GetMapping("/get")
    @ResponseBody
    public Result GetAppeal(){

        List<MaterialAppeal> materialAppealListAppeal  = materialAppealMap.GetMaterialAppeals();

        return ResultUtil.success(materialAppealListAppeal);
    }


    @PostMapping("/save")
    @ResponseBody
    public Result SaveAppeal(@RequestBody MaterialAppeal materialAppeal){


        String uuid = UUID.randomUUID().toString();


        Result result =  new RedisLockTemplate() {
            @Override
            public Result ActionInLock() {
                return materialAppealService.SaveMaterialAppeal(materialAppeal);
            }
        }.executeSafely(redisService, uuid ,materialAppeal.getMaterialId());




        return result;
    }

    @GetMapping("/delete/{id}")
    @ResponseBody
    public Result DeleteAppeal(@PathVariable int id){
        materialAppealMap.DeleteMaterialAppeal(id);
        return ResultUtil.success("删除成功");
    }

    @PostMapping("/update")
    public Result UpdateAppeal(@RequestBody MaterialAppeal materialAppeal){


      Result res =  materialAppealService.UpdateMaterialAppeal(materialAppeal);

        return res;
    }


    @PostMapping("/updateState")
    public Result UpdateState(@RequestBody MaterialAppealParam materialAppealParam){

        //回滚 回退相关器材
         Result res =    materialAppealService.UpdateState(materialAppealParam);
        if(res.getCode()!= 200){
            return res;
        }
        return ResultUtil.success(materialAppealParam);
    }


    @GetMapping("/search")
    public Result SearchAppeal(@RequestParam(name = "adminName",defaultValue="") String adminName, @RequestParam(name = "materialName", defaultValue="") String materialName){
       
        if(StringUtils.isEmpty(adminName)&&StringUtils.isEmpty(materialName)){
            return ResultUtil.success(materialAppealMap.GetMaterialAppeals());
        }



        return ResultUtil.success( materialAppealService.SearchMaterialAppeal(adminName,materialName));
    }


    @GetMapping("/getCount")
    public Result GetCount(){


       int[] arrs = new int[2];
       List<MaterialAppeal> b = materialAppealMap.CountBorrow();
        List<MaterialAppeal> r =materialAppealMap.CountReturn();
        arrs[0] = b.size();
        arrs[1] = r.size();


        return ResultUtil.success( arrs);
    }

    @GetMapping("/getUsage")
    public Result GeyUsage(){

        return ResultUtil.success( materialAppealMap.CountUsage());
    }



}
